package br.com.sgo.util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import br.com.sgo.dao.UserDao;
import br.com.sgo.model.User;
import br.com.sgo.util.hibernate.ServiceRegistryCreator;
import br.com.sgo.util.hibernate.SessionCreator;

public class PopulateDefaultDatabase {

	public static void main(String[] args) {

		Session session = getSession(); 
		populateUsers(session);
	}

	private static Session getSession() {
		Configuration cfg = new Configuration().configure();
		ServiceRegistryCreator serviceRegistry = new ServiceRegistryCreator(cfg);
		serviceRegistry.create();
		
		SessionFactory factory = cfg.buildSessionFactory(serviceRegistry.getInstance()); 
	
		SessionCreator sessionCreator = new SessionCreator(factory);
		sessionCreator.create();
		
		Session session = sessionCreator.getInstance();
		return session;
	}

	private static void populateUsers(Session session) {
		UserDao dao = new UserDao(session);
		
		User user = new User();
		user.setName("Admin");
		user.setEmail("admin@admin.com.br");
		user.setPassword("admin");

		dao.save(user);
	}

}
